local GenieList = GenieList

-- FuBar options.
GenieList.name = 'GenieList'
GenieList.title = 'GenieList'
GenieList.hasIcon = 'Interface\\AddOns\\GenieList\\GenieList'

GenieList.options = {
  type = 'group',
  args = {
    item_menu = {
      type = 'group',
      name = 'Items',
      desc = 'List of items in the wishlist.',
      order = 1,
      args = {},
      cmdHidden = true,
    },
    item_highlight = {
      type = 'toggle',
      name = 'Highlight items',
      desc = 'Toggles highlighting of items in quest log or roll frame.',
      order = 2,
      get = function()
        return GenieList.db.profile.highlight_item
      end,
      set = function()
        GenieList.db.profile.highlight_item =
            (not GenieList.db.profile.highlight_item)
      end,
    },
    show_acquired_items = {
      type = 'toggle',
      name = 'Show acquired items',
      desc = 'Toggles showing of acquired items in wishlist.',
      order = 3,
      get = function()
        return GenieList.db.profile.show_acquired_items
      end,
      set = function()
        GenieList.db.profile.show_acquired_items =
            (not GenieList.db.profile.show_acquired_items)
        GenieList:Refresh()
      end,
    },
    add = {
      type = 'text',
      name = 'Add item',
      desc = 'Add an item to the wishlist.',
      usage = 'Add an item to the wishlist.',
      get = false,
      set = function(item)
        GenieList:AddItem(GenieList.db.char.items, item)
        GenieList:Refresh()
      end,
      order = 4,
    },
  },
}

function GenieList:RegisterMenu()
  self:UpdateItemMenu()
	self:RegisterChatCommand({ '/genielist' }, self.options)
  self.OnMenuRequest = self.options
end


function GenieList:UpdateItemMenu()
  local item_menu = {}

  local index, item
  for index, item in ipairs(self:GetSortedItems(self.db.char.items)) do
    -- Get additional info on the item based on its id.
		local _, _, _, _, _, _, _, _, _, item_texture = GetItemInfo(item.id)

    item_texture = item_texture or ''

    -- If the item is acquired, we'll gray its name out.
    local item_link = item.name
    if (item.acquired) then
      item_link = '|cff9d9d9d'..item.name..'|r'
    end

    -- For each item, we have a submenu for manipulating the item.
    -- Currently the only supported action is removing the item.
    local item_submenu = {}
    table.insert(item_submenu, {
      type = 'execute',
      name = 'Remove Item',
      desc = 'Remove the item from the wishlist.',
      func = function()
        self:RemoveItem(self.db.char.items, item.name)
        self:Refresh()
      end,
    })

    -- Insert the menu item into the main menu.
    if ((not item.acquired) or self.db.profile.show_acquired_items) then
      table.insert(item_menu, {
        type = 'group',
        name = item_link,
        desc = item.name,
        order = index,
        icon = item_texture,
        args = item_submenu,
      })
    end
  end

  self.options.args.item_menu.args = item_menu
end
